package com.oriental.manage.service.payment.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oriental.manage.core.enums.ResponseResult;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.utils.DateUtils;
import com.oriental.manage.core.utils.SessionUtils;
import com.oriental.manage.dao.base.CommonMapper;
import com.oriental.manage.dao.payment.OutlineOrderMapper;
import com.oriental.manage.pojo.payment.OutlineOrder;
import com.oriental.manage.service.payment.IOutlineOrderService;
import com.oriental.paycenter.external.api.operations.PayFacade;
import com.oriental.paycenter.external.model.ResponseDTO;
import com.oriental.paycenter.external.model.pay.ReverseOrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * Created by aaa on 2017/5/17.
 */
@Slf4j
@Service
public class OutlineOrderServiceImpl implements IOutlineOrderService {

    @Autowired
    private OutlineOrderMapper outlineOrderMapper;
    @Autowired
    private CommonMapper commonMapper;

    @Reference(registry = "payCenter", timeout = 60000)
    private PayFacade payFacade;

    @Override
    public Pagination<OutlineOrder> selectSelective(Pagination<OutlineOrder> pagination) {
        PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());

        List<OutlineOrder> list = outlineOrderMapper.selectSelective(pagination.getQueryBean());
        PageInfo<OutlineOrder> pageInfo = new PageInfo(list);
        pagination.setRowCount(pageInfo.getTotal());
        pagination.setList(pageInfo.getList());
        return pagination;
    }

    @Override
    public boolean post(OutlineOrder outlineOrder) {

        outlineOrder.setId(commonMapper.getCommonId("seq_common_8"));
        outlineOrder.setCreator(SessionUtils.getUserName());
        int count = outlineOrderMapper.insertSelective(outlineOrder);
        if (count > 0)
            return true;

        return false;
    }

    @Override
    public boolean put(OutlineOrder outlineOrder) {

        outlineOrder.setAuditor(SessionUtils.getUserName());
        outlineOrder.setAuditDate(new Date());
        int count = outlineOrderMapper.updateByPrimaryKeySelective(outlineOrder);
        if (count > 0)
            return true;

        return false;
    }

    @Override
    public void sendTransaction(OutlineOrder outlineOrder) {

        outlineOrder = outlineOrderMapper.selectByPrimaryKey(outlineOrder.getId());

        ReverseOrderDTO dto = new ReverseOrderDTO();

        try {
            BeanUtils.copyProperties(dto, outlineOrder);

            dto.setOrderDate(DateUtils.format(outlineOrder.getOrderDate(), DateUtils.DATETIMESFORMAT));
            dto.setPaySettleDate(DateUtils.format(outlineOrder.getPaySettleDate(), DateUtils.DATESHORTFORMAT));

            ResponseDTO<String> res = payFacade.reverseOrder(dto);

            if (res.isSuccess()) {
                if (StringUtils.equals(res.getResult(), ResponseResult.SUCCESS.getCode())) {
                    outlineOrder.setStatus("03");
                } else {
                    outlineOrder.setStatus("04");
                    outlineOrder.setComment(res.getResult());
                }
            } else {
                outlineOrder.setStatus("04");
                outlineOrder.setComment(res.getErrorCode() + res.getErrorMsg());
            }

        } catch (Exception e) {
            log.error("交易反录失败", e);
            outlineOrder.setStatus("04");
            outlineOrder.setComment(e.getMessage());
        }

        outlineOrderMapper.updateByPrimaryKeySelective(outlineOrder);
    }
}
